package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.StationAlarmE;
import com.infore.airsupervise.biz.StationAlarmBiz;
import com.infore.airsupervise.biz.impl.StationAlarmBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.SwipeRecyclerView;
import com.infore.airsupervise.utils.JumpUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import info.hoang8f.android.segmented.SegmentedGroup;
import retrofit2.Call;

/**
 * @features: 报警 实时与历史列表界面
 * @author: WuTao
 * @date: 2017/3/7 13:36
 */
public class AlarmActivity
        extends BaseActivity
        implements SwipeRecyclerView.OnActionListener {

    @BindView(R.id.segment_group)
    SegmentedGroup segmentGroup;
    @BindView(R.id.segment_alarm)
    SegmentedGroup segmentAlarm;
    @BindView(R.id.swipeRecyclerView)
    SwipeRecyclerView swipeRecyclerView;

    private BaseQuickAdapter<StationAlarmE, BaseViewHolder> quickAdapter;
    private List<StationAlarmE> alarmEList = new ArrayList<>();

    /**
     * 分类标识
     */
    private static final int ALARM_TYPE_REAL = 1;//实时
    private static final int AlARM_TYPE_HISTORY = 2;//历史
    private static final int ALARM_LEVEL_FIRST = 1;
    private static final int ALARM_LEVEL_SECOND = 2;
    private static final int ALARM_LEVEL_THREE = 3;
    private static final int ALARM_LEVEL_FOUR = 4;
    private static final int ALARM_LEVEL_ALL = 5;
    private int type;//实时 、历史
    private int alarmlevel;//报警级别
    private int pageNum;//分页标识

    private StationAlarmBiz stationAlarmBiz;//业务类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_realandhistory);
        ButterKnife.bind(this);
        stationAlarmBiz = new StationAlarmBizImpl();
        initViews();
        initRecycleView();
    }

    /**
     * 初始化其他控件 设置
     */
    private void initViews() {
        type = ALARM_TYPE_REAL;
        alarmlevel = ALARM_LEVEL_ALL;
        segmentGroup.setOnCheckedChangeListener(checkListener);  //实时 历史
        segmentAlarm.setOnCheckedChangeListener(checkListener); //报警级别单选组
    }


    /**
     * 设置列表和适配器
     */
    private void initRecycleView() {

        //适配器
        quickAdapter = new BaseQuickAdapter<StationAlarmE, BaseViewHolder>(R.layout.item_alarm, alarmEList) {
            @Override
            protected void convert(BaseViewHolder helper, StationAlarmE item) {
                helper.setText(R.id.tv_alarm_name, item.getStationName())
                        .setText(R.id.tv_alarm_content, item.getDiscrib())
                        .setText(R.id.tv_alarm_time, item.getTime());
                ImageView iv_icon = helper.getView(R.id.view_indicator);
                if (item.getWarningLevel() == null || item.getWarningLevel().isEmpty()) {
                 iv_icon.setImageResource(R.drawable.alarm_0);
                } else if (item.getWarningLevel().equals("一级")) {
                    iv_icon.setImageResource(R.drawable.alarm_1);
                } else if (item.getWarningLevel().equals("二级")) {
                    iv_icon.setImageResource(R.drawable.alarm_2);
                } else if (item.getWarningLevel().equals("三级")) {
                    iv_icon.setImageResource(R.drawable.alarm_3);
                } else if (item.getWarningLevel().equals("四级")) {
                    iv_icon.setImageResource(R.drawable.alarm_4);
                }
            }
        };

        // 设置recyclerView
        swipeRecyclerView.setAdapter(quickAdapter);
        swipeRecyclerView.setActionListener(this);

        // 首次自动刷新
        swipeRecyclerView.getSwipeLayout().setRefreshing(true);
        swipeRecyclerView.onRefresh();
    }

    /**
     * 获取报警信息
     */
    private void getAlarmDatas(final boolean isRefresh) {
        Call call = stationAlarmBiz.sendRealAlarmRequest(
                "", type, alarmlevel, pageNum, new OnServerResponseListener<List<StationAlarmE>>() {
                    @Override
                    public void onSucceed(List<StationAlarmE> result, String desc) {
                        swipeRecyclerView.commonSuccessHandle(isRefresh, result.size());
                        pageNum++;

                        if(isRefresh) {
                            alarmEList.clear();
                        }

                        alarmEList.addAll(result);
                        swipeRecyclerView.getAdapter().notifyDataSetChanged();
                    }

                    @Override
                    public void onFailed(int errorCode, String title, String desc) {
                        swipeRecyclerView.commonFailedHandle(isRefresh, errorCode, title, desc);
                    }
                });
        requests.add(call);
    }

    /**
     * 搜索按钮
     */
    @OnClick({R.id.titlebar_right_menu, R.id.titlebar_left_menu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_right_menu:
                JumpUtil.jumpFromTo(this, AlarmSearchActivity.class);
                break;
            case R.id.titlebar_left_menu:
                finish();
                break;
        }
    }

    /**
     * segment 单选组
     */
    private RadioGroup.OnCheckedChangeListener checkListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i) {
                case R.id.rb_real:
                    type = ALARM_TYPE_REAL;
                    break;
                case R.id.rb_history:
                    type = AlARM_TYPE_HISTORY;
                    break;
                case R.id.rb_first:
                    alarmlevel = ALARM_LEVEL_FIRST;
                    break;
                case R.id.rb_second:
                    alarmlevel = ALARM_LEVEL_SECOND;
                    break;
                case R.id.rb_thred:
                    alarmlevel = ALARM_LEVEL_THREE;
                    break;
                case R.id.rb_four:
                    alarmlevel = ALARM_LEVEL_FOUR;
                    break;
                case R.id.rb_all:
                    alarmlevel = ALARM_LEVEL_ALL;
            }
            //发送请求
            swipeRecyclerView.getSwipeLayout().setRefreshing(true);
            swipeRecyclerView.onRefresh();
        }
    };

    @Override
    public void onRefresh() {
        pageNum = 1;
        getAlarmDatas(true);
    }

    @Override
    public void onLoadMore() {
        getAlarmDatas(false);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        JumpUtil.jumpFromTo(
                ctx, NewAQIActivity.class, Constants.INTENT_STR_KEY, alarmEList.get(position).getMn());
    }

}
